//
//  UIView+Extension.m
//  01-黑酷
//
//  Created by apple on 14-6-27.
//  Copyright (c) 2014年 heima. All rights reserved.
//

#import "UIView+Extension.h"

@implementation UIView (Extension)

-(void)setZq_x:(CGFloat)zq_x
{
    CGRect frame = self.frame;
    frame.origin.x = zq_x;
    self.frame = frame;
}

- (CGFloat)zq_x {
    return self.frame.origin.x;
}

-(void)setZq_y:(CGFloat)zq_y
{
    CGRect frame = self.frame;
    frame.origin.y = zq_y;
    self.frame = frame;
}

- (CGFloat)zq_y {
    return self.frame.origin.y;
}

-(void)setZq_width:(CGFloat)zq_width
{
    CGRect frame = self.frame;
    frame.size.width = zq_width;
    self.frame = frame;
}

- (CGFloat)zq_width {
    return self.frame.size.width;
}

-(void)setZq_height:(CGFloat)zq_height
{
    CGRect frame = self.frame;
    frame.size.height = zq_height;
    self.frame = frame;
}

- (CGFloat)zq_height
{
    return self.frame.size.height;
}

-(void)setZq_size:(CGSize)zq_size
{
//    self.width = size.width;
//    self.height = size.height;
    CGRect frame = self.frame;
    frame.size = zq_size;
    self.frame = frame;
}

- (CGSize)zq_size {
    return self.frame.size;
}

-(void)setZq_origin:(CGPoint)zq_origin
{
    CGRect frame = self.frame;
    frame.origin = zq_origin;
    self.frame = frame;
}

- (CGPoint)zq_origin {
    return self.frame.origin;
}

-(void)setZq_centerX:(CGFloat)zq_centerX
{
    CGPoint center = self.center;
    center.x = zq_centerX;
    self.center = center;
}

- (CGFloat)zq_centerX {
    return self.center.x;
}

-(void)setZq_centerY:(CGFloat)zq_centerY
{
    CGPoint center = self.center;
    center.y = zq_centerY;
    self.center = center;
}

- (CGFloat)zq_centerY {
    return self.center.y;
}


- (CGFloat)zq_left {
    return self.frame.origin.x;
}
-(void)setZq_left:(CGFloat)zq_left
{
    CGRect frame = self.frame;
    frame.origin.x = zq_left;
    self.frame = frame;
}

- (CGFloat)zq_top {
    return self.frame.origin.y;
}

-(void)setZq_top:(CGFloat)zq_top
{
    CGRect frame = self.frame;
    frame.origin.y = zq_top;
    self.frame = frame;
}

- (CGFloat)zq_right {
    return self.frame.origin.x + self.frame.size.width;
}

-(void)setZq_right:(CGFloat)zq_right
{
    CGRect frame = self.frame;
    frame.origin.x = zq_right - frame.size.width;
    self.frame = frame;
}

- (CGFloat)zq_bottom {
    return self.frame.origin.y + self.frame.size.height;
}

-(void)setZq_bottom:(CGFloat)zq_bottom
{
    CGRect frame = self.frame;
    frame.origin.y = zq_bottom - frame.size.height;
    self.frame = frame;
}



- (void)addTarget:(id)target action:(SEL)action;
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:target
                                                                         action:action];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:tap];
}


- (BOOL)isShowingOnKeyWindow
{
    // 主窗口
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    // 以主窗口左上角为坐标原点, 计算self的矩形框
    CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview];
    CGRect winBounds = keyWindow.bounds;
    
    // 主窗口的bounds 和 self的矩形框 是否有重叠
    BOOL intersects = CGRectIntersectsRect(newFrame, winBounds);
    
    return !self.isHidden && self.alpha > 0.01 && self.window == keyWindow && intersects;
}

+ (instancetype)viewFromXib
{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}
@end
